﻿using RescueApp.Data;
using RescueApp.Model;

using EFCoreRepository.DbContexts;
using EFCoreRepository.Extensions;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using System.Data;
using Microsoft.Extensions.DependencyInjection;



namespace RescueApp.CreatDb
{
    internal class Program
    {
        static void Main(string[] args)
        {
            const string connectionString = "Server=LAPTOP-1BOQI73G;Database=Rescue;TrustServerCertificate=True;Integrated Security=True;";
            
            IServiceCollection services = new ServiceCollection();
            services.AddDbContext<RescueContext>(options =>
                        options.UseSqlServer(connectionString));
            services.AddSqlServer<RescueContext>(connectionString);
            //services.AddSingleton<TodoContext>();
           // services.AddSingleton<RescueItemRepos>();
            var serviceProvider = services.BuildServiceProvider();
            RescueContext db = serviceProvider.GetRequiredService<RescueContext>();
            //TodoItemRepos repos = serviceProvider.GetRequiredService<TodoItemRepos>();

            //RescueContext db = new RescueContext();
            //RescueApplyRepos repos = new RescueApplyRepos();

           

            //orm
            InsertWithORMDemo(db);
            UpdateWithORMDemo(db);
            DeleteWithORMDemo(db);

            
        }

       
        //查询
        static bool IsExistsWithORM(RescueContext db, int Id)
        {
            var rescueApplies = db.RescueApplies.Where(x => x.RescueID == Id);
            if (rescueApplies.Any())
            {
                return true;
            }
            return false;
        }

        static void InsertWithORMDemo(RescueContext db)
        {
            RescueApply item = new RescueApply();
            item.RescueID = 13;
            item.LicenseID = "1234-5678";
            item.CarType = "true";
            item.DriverLicenseID = "0123456789";
            item.Phone = "1234567890";
            item.RescueLocation = "wuhan";
            item.Destination = "ccnu";
            item.FillingDate= DateTime.Now;

            if (IsExistsWithORM(db, item.RescueID))
            {
                Console.WriteLine("The record already exists");
                return;
            }
            else
            {
                db.RescueApplies.Add(item);
                db.SaveChanges();
                Console.WriteLine("新增成功");
            }
        }

        static void UpdateWithORMDemo(RescueContext db)
        {
            int Id= 11;

            if (!IsExistsWithORM(db, Id))
            {
                Console.WriteLine("The record not existing");
                return;
            }
            else
            {
                var entity = db.RescueApplies.FirstOrDefault(x => x.RescueID == Id);
                if (entity != null)
                {
                    entity.RescueID = 11;
                    entity.LicenseID = "1234-5678";
                    entity.CarType = "false";
                    entity.DriverLicenseID = "0123456789";
                    entity.Phone = "1234567890";
                    entity.RescueLocation = "wuhan";
                    entity.Destination = "ccnu";
                    entity.FillingDate = DateTime.Now;

                    db.SaveChanges();
                }
                Console.WriteLine("修改成功");
            }
        }

        static void DeleteWithORMDemo(RescueContext db)
        {
            int Id = 20;

            if (!IsExistsWithORM(db, Id))
            {
                Console.WriteLine("The record not existing");
                return;
            }
            else
            {
                var entity = db.RescueApplies.FirstOrDefault(x => x.RescueID == Id);
                if (entity != null)
                {
                    db.RescueApplies.Remove(entity);
                    db.SaveChanges();
                }
                Console.WriteLine("删除成功");
            }
        }

        


    }
}
